﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace rInvoice
{
    public partial class Constante : UserControl
    {
        ServerObject mServerObject = null;
        bool mCreatingNew = false;
        
        string mIdentificator = string.Empty;
        string mSinonim = string.Empty;
        string mComent = string.Empty;

        int mConstantID = 0;
        
        public string Sinonim
        {
            get { return mSinonim; }
            set { mSinonim = value; }
        }

        public string Coment
        {
            get { return mComent; }
            set { mComent = value; }
        }
        
        public Constante()
        {
            InitializeComponent();
            mServerObject = new ServerObject();
        }

        public bool CreateNew
        {
            get
            {
                return mCreatingNew;
            }

            set
            {
                mCreatingNew = value;
            }
        }           

        private void SetConstant()
        {
            bool result = false;
            try
            {
                if (mCreatingNew)
                {
                    result = mServerObject.AddConstant(mIdentificator, mSinonim, mComent);
                }
                else
                {
                    result = mServerObject.UpdateConstant(mConstantID, mIdentificator, mSinonim, mComent);
                }

                if (result)
                {
                    MessageBox.Show("Succed saved data.\r");
                }
                else
                {
                    MessageBox.Show("Failure saving data.\r");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failure saving data.\r" + ex.Message);
            }
            FillGridView();
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            mCreatingNew = true;
            SetConstant();
        }

        private void identificatorTextBox_TextChanged(object sender, EventArgs e)
        {
            mIdentificator = identificatorTextBox.Text.Trim();
        }

        private void sinonimTextBox_TextChanged(object sender, EventArgs e)
        {
            mSinonim = sinonimTextBox.Text.Trim();
        }

        private void comentTextBox_TextChanged(object sender, EventArgs e)
        {
            mComent = comentTextBox.Text.Trim();
        }

        private void Constante_Load(object sender, EventArgs e)
        {
            FillGridView();
        }

        private void FillGridView()
        {
            DataTable dataTable = mServerObject.GetConstantList();
            constantsGridView.DataSource = dataTable;
        }

    }
}
